<template>
  <van-nav-bar
    title="委托单确认"
    left-text="返回"
    left-arrow
    @click-left="onClickLeft"
  />
  <div class="wrap">
    <div class="list-wrapper">
      <ul class="header">
        <li class="column-large">客户：{{ info.custname }}</li>
        <!--<li class="column-large">总费用：{{ info.totalamount }}</li>-->
      </ul>
      <ul class="header">
        <li class="column-large">委托单号：{{ info.orderno }}</li>
      </ul>
    </div>
    <div class="list-wrapper">
      <ul class="header">
        <li class="column-large">样品</li>
        <li>送样编号</li>
        <li>检验项目</li>
        <!--<li class="column-large">检查费用</li> -->
      </ul>
      <van-list
        v-model:loading="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <ul v-for="(item, index) in info?.orderitems" :key="item.id">
          <li class="column-large">
            {{ item?.testsample }}
          </li>
          <li>{{ item?.sampleid }}</li>
          <li>{{ item?.matname }}</li>
          <!--	<li class="column-large">{{ item?.amount }}</li>-->
        </ul>
      </van-list>
      <div>
        <span style="margin-right: 12px">
          <van-button plain hairline type="primary" size="small" @click="toSign"
            >委托单确认</van-button
          >
        </span>
        <span style="margin-left: 12px">
          <van-button
            plain
            hairline
            type="primary"
            size="small"
            @click="toOrdSign"
            >使用上次签名确认</van-button
          >
        </span>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { useRoute, useRouter } from "vue-router";
import {
  showConfirmDialog,
  showToast,
  showLoadingToast,
  closeToast,
} from "vant";

import service from "@/api";

const loading = ref(false);
const finished = ref(false);

const route = useRoute();
const router = useRouter();
const show = ref(false);

const info = ref(null);
const orderno = route?.query?.orderno || "";
const openId = route?.query?.openId || "";

const onLoad = async () => {
  debugger;
  showLoadingToast({
    duration: 0,
    forbidClick: true,
    message: "查询中",
  });
  try {
    const res = service
      .queryOrders({
        itype: "saleorder_query", //订单查询
        info: {
          orderNo: orderno,
          openId: openId,
        },
      })
      .then((res) => {
        console.log(res);
        closeToast();
        if (res.status == "success") {
          info.value = res.data && res.data[0];
          console.log(info.value.orderitems);
        } else {
          closeToast();
          showToast(res.message);
        }
      });
    finished.value = true;
  } catch (error) {
    console.error("绑定请求失败：", error);
  }
};
onLoad();

const onClickLeft = () => {
  router.back();
};

const openFile = () => {
  let url = "https://view.officeapps.live.com/op/view.aspx";
  window.open(url); // window.location.href=url
};

const check = () => {
  show.value = true;
};

const toSign = () => {
  if (!info.value.signfilepk) {
    router.push({
      path: "/sign",
      query: {
        orderno: info.value.orderno,
        openId: openId,
      },
    });
  } else {
    showToast("已存在签名");
  }
};
const toOrdSign = async () => {
  try {
    if (!info.value.signfilepk) {
      service
        .uploadSign({
          itype: "saleorder_upload", //订单查询
          info: {
            orderNo: orderno,
            openId: openId,
            fileData: "",
            useBeforeSign: "Y",
          },
        })
        .then((re) => {
          if (re.status == "success") {
            showToast({
              message: "上传签字图片成功",
              duration: 3000,
            });
            setTimeout(() => {
              router.back();
            }, 1500);
          } else {
            closeToast();
            showToast(re.message);
          }
        });
    } else {
      showToast("已存在签名");
    }
  } catch (error) {
    console.error(error);
  }
};
</script>

<style>
.content {
  .van-cell {
    padding-right: 24px !important;
  }

  .van-cell__title {
    flex: 0 0 20% !important;
  }
}
</style>
<style scoped>
#app {
  padding: 0 !important;
}

body {
  display: block !important;
}

.wrap {
  padding: 0;
  width: 100vw;
  height: 100vh;
}

.title-wrap {
  width: 120px;
  text-align: left;
}

.content-action {
  display: flex;
  padding: 0 24px;
  flex-direction: row-reverse;
}

.van-collapse-item__content {
  padding: 0;
}

.popup-content {
  max-width: 750px;
  min-height: 100px;
}

.list-wrapper {
  flex: 1;
  background: white;
  overflow: auto;
  margin: 5px 15px;
  position: relative;
}

.list-wrapper .header {
  background: #5b87ff;
  color: white;
  border: none;
  position: sticky;
  top: 0;
  z-index: 1;
}

ul {
  padding: 10px;
  border-bottom: 1px solid #cacaca;
  display: flex;
  justify-content: space-between;
  flex-wrap: nowrap;
}

ul li {
  width: 100px;
  font-weight: 500;
  display: inline-block;
}

.column-large {
  width: 160px;
}
</style>
